package com.ncr.travel.appmgr.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.ncr.travel.appmgr.model.shared.Graphics;
import com.ncr.travel.appmgr.model.shared.Product;
import com.ncr.travel.appmgr.model.shared.ProductCategory;
import com.ncr.travel.appmgr.model.shared.Rule;
import com.ncr.travel.appmgr.model.shared.Theme;
import com.ncr.travel.appmgr.service.ApplicationManagerService;
import com.ncr.travel.appmgr.service.mock.MockDatabase;
import com.ncr.travel.appmgr.service.model.ProductCategoriesResponse;
import com.ncr.travel.appmgr.service.model.ProductsResponse;
import com.ncr.travel.appmgr.service.model.ScreenConfiguration;
import com.ncr.travel.appmgr.service.provider.GraphicsProvider;
import com.ncr.travel.appmgr.service.provider.ThemeProvider;

@Component
public class MockApplicationManagerService implements ApplicationManagerService {

	private final MockDatabase database;
	private final ThemeProvider themeProvider;
	private final GraphicsProvider graphicsProvider;

	@Autowired
	public MockApplicationManagerService(MockDatabase database, ThemeProvider themeProvider, GraphicsProvider graphicsProvider) {
		super();
		this.database = database;
		this.themeProvider = themeProvider;
		this.graphicsProvider = graphicsProvider;
	}

	@Override
	public ProductsResponse getProducts(String userName) throws Exception {
		ProductsResponse response = new ProductsResponse();
		List<Product> products = database.getProducts();
		response.setProducts(products);
		return response;
	}

	@Override
	public ProductCategoriesResponse getProductCategories(String productName) {
		ProductCategoriesResponse response = new ProductCategoriesResponse();
		List<ProductCategory> productCategories = database.getProductCategories();
		response.setProductCategories(productCategories);
		return response;
	}

	@Override
	public Rule getRulesByScreenName(String screenName) {
		Integer screenId = database.lookupScreenId(screenName);
		if (screenId == null)
			return null;
		List<Rule> rules = database.getRulesByScreenId(screenId);
		if (rules.isEmpty())
			return null;
		return rules.get(0);
	}

	@Override
	public List<Theme> getListOfThemes() {
		return themeProvider.getThemes();
	}

	@Override
	public Theme getThemeByUUID(String themeId) {
		return themeProvider.getThemeByUUID(themeId);
	}

	@Override
	public Theme getThemeByNameVersion(String themeName, String version) {
		return themeProvider.getThemeByNameVersion(themeName, version);
	}

	@Override
	public ScreenConfiguration findConfigurationByKeyAndPath(String key, String path) throws Exception {
		String screen = path.substring(path.lastIndexOf("&")+1, path.length());
		Integer screenId = database.lookupScreenId(screen);
		return database.getScreenConfiguration(screenId);
	}

	@Override
	public void save(ScreenConfiguration screenConfiguration, String path) throws Exception {
		database.saveFunctions(screenConfiguration);
		database.saveRules(screenConfiguration);
		database.saveText(screenConfiguration);
	}

	@Override
	public void addImageToCollection(long screenId, long graphicsId, MultipartFile imageFile) {
		graphicsProvider.addImageToCollection(screenId, graphicsId, imageFile);
	}

	@Override
	public void updateGraphicsCollection(long screenId, List<Graphics> graphics) {
		graphicsProvider.updateGraphicsCollection(screenId, graphics);
	}

}
